package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import de.hdodenhof.circleimageview.CircleImageView;
import liumengbaio.lolzhangyoubao.Activity.Adapter.ComentItemAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.ComentInfoUp;
import liumengbaio.lolzhangyoubao.Activity.InFo.FindFriendItemUp;
import liumengbaio.lolzhangyoubao.R;

public class PersonInfoActivity extends AppCompatActivity {
    private ListView person_info_listview;
    private ComentInfoUp itemdown;
    private FindFriendItemUp itemUp;
    private ComentItemAdapter adapter;
    private String id = "";
    private CircleImageView head_icon;
    private TextView tv_nicker_name, tv_lol_name, tv_time, tv_do_black_voice, tv_in_the_city, tv_fight_notice, tv_good, tv_coment_item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_info);
        id = getIntent().getExtras().getString("id");
        iniView();
        getDataFromWeb();
    }

    private void getDataFromWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://lol.zhangyoubao.com/apis/rest/UgcsService/companionComments?page=1" +
                        "&id=" + id + "&userid=15863305&i_=Lf2he/rdpOPtR1PwqdUiuwS64wm6xMV8rBpebf4NEuI=&de" +
                        "viceId=Lf2he/rdpOPtR1PwqdUiuwS64wm6xMV8rBpebf4NEuI=&t_=1474011612458&p_=9719&" +
                        "v_=400902&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=17&cha=baiduMartket&u" +
                        "_=15863305&modle_=vivo%20Y15T&api=UgcsService.companionComments&time=14740114" +
                        "75&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0" +
                        "&nonce=212650&secretSignature=SuIPXrwefbZLVBUNy1rhvIXi/ho=";
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(PersonInfoActivity.this, "数据获取错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        itemdown = gson.fromJson(result, ComentInfoUp.class);
                        if (itemdown.getData().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url_up = "http://lol.zhangyoubao.com/apis/rest/UgcsService/companionDegree?id=" + id + "&userid=15863305&i_=Lf2he/rdpOPtR1PwqdUiuwS64wm6xMV8rBpebf4NEuI=&deviceId=Lf2he/rdpOPtR1PwqdUiuwS64wm6xMV8rBpebf4NEuI=&t_=1474012904270&p_=15155&v_=400902&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=17&cha=baiduMartket&u_=15863305&modle_=vivo%20Y15T&api=UgcsService.companionDegree&time=1474012768&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=763760&secretSignature=iVu5UeMmrr8ahwveNZYiV12sDgY=";
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url_up)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(PersonInfoActivity.this, "数据获取错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        itemUp = gson.fromJson(result, FindFriendItemUp.class);
                        handler_up.sendEmptyMessage(1);
                    }
                });
            }
        }).start();
    }

    private void iniView() {
        findViewById(R.id.head_left_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        person_info_listview = (ListView) findViewById(R.id.person_info_listview);
        head_icon = (CircleImageView) findViewById(R.id.head_icon);
        tv_nicker_name = (TextView) findViewById(R.id.tv_nicker_name);
        tv_lol_name = (TextView) findViewById(R.id.tv_lol_name);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_do_black_voice = (TextView) findViewById(R.id.tv_do_black_voice);
        tv_in_the_city = (TextView) findViewById(R.id.tv_in_the_city);
        tv_fight_notice = (TextView) findViewById(R.id.tv_fight_notice);
        tv_good = (TextView) findViewById(R.id.tv_good);
        tv_coment_item = (TextView) findViewById(R.id.tv_coment_item);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                adapter = new ComentItemAdapter(PersonInfoActivity.this, itemdown);
                person_info_listview.setAdapter(adapter);
            }
        }
    };
    private Handler handler_up = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Picasso.with(PersonInfoActivity.this).load(itemUp.getData().getAvatar()).into(head_icon);
                tv_nicker_name.setText(itemUp.getData().getNickname());
                tv_lol_name.setText(itemUp.getData().getSummoner());
                tv_time.setText(itemUp.getData().getCreated());
                tv_do_black_voice.setText("开黑语音:不确定");
                if (itemUp.getData().getCity() != null) {
                    tv_in_the_city.setText("所在城市:" + itemUp.getData().getCity());
                }else {
                    tv_in_the_city.setText("所在城市:未知");
                }
                tv_fight_notice.setText("约战宣言:" + itemUp.getData().getSlogan());
                tv_good.setText(itemUp.getData().getGood());
                tv_coment_item.setText(itemUp.getData().getComment());
            }
        }
    };
}
